/*
 * parserimport.c
 *
 *  Created on: 2013-5-13
 *      Author: xkaying
 */

#include "parserimport.h"
#include "lexer.h"
#include "../typedef/token.h"
#include "../typedef/package.h"
#include "../typedef/import.h"

#include <assert.h>
#include "parser.h"

int ParserImport(Lexer *lexer, Package *package)
{
	assert(lexer);
	assert(package);
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	Import *import;
	import = 0;
	while (ptoken) {
		switch (state)
		{
			case 0: {
				//包名字
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 1;
						//
						import = ImportInit();
						ImportAppend(import,ptoken->name);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:包名称格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser import状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Get: {
						state = 2;
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//表示包定义结束,进入包内容开始
					case TokenType_End:
					case TokenType_Comment: {
						PackageAppendImport(package,import);
						return 1;
					}
					default: {
						printf("第%d行:包名称格式错误.\n",ptoken->line);
						return 0;
					}
				}
				printf("parser import状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 1;
						//生成该包
						ImportAppend(import, ptoken->name);
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:包名称格式错误.\n",ptoken->line);
						return 0;
					}
				}
				printf("parser import状态机错误.状态%d.\n", state);
				return 0;
			}
			default:{
				printf("parser import状态机错误.状态:default.\n");
				return 0;
			}
		}
	}
	printf("不完整的包导入语句.\n");
	return 0;
}
